package com.qidianit.mes.production.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qidianit.mes.common.Constant;
import com.qidianit.mes.common.pojo.MesResult;
import com.qidianit.mes.pojo.ProcessStep;
import com.qidianit.mes.production.mapper.ProcessStepMapper;
import com.qidianit.mes.production.service.ProcessStepService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author 戴着假发的程序员
 * @company 起点编程
 * @date 2025/8/4 10:38
 * @description
 */
@Service
public class ProcessStepServiceImpl extends ServiceImpl<ProcessStepMapper, ProcessStep> implements ProcessStepService {
    @Override
    public MesResult<Integer> deleteBatch(String[] stepIds) {
        UpdateWrapper< ProcessStep> wrapper = new UpdateWrapper<>();
        wrapper.in("step_id", stepIds);
        wrapper.set("delete_flag",1);
        return MesResult.success(this.baseMapper.delete(wrapper));
    }

    @Override
    public MesResult<Integer> deleteById(Integer stepId) {
        ProcessStep tempStep = this.getById(stepId);
        if (tempStep == null){
            tempStep.setDeleteFlag(1);
            return this.updateById(tempStep) ? MesResult.success(1) : MesResult.error();
        }
        return MesResult.error(Constant.ERROR_CODE,"要删除的数据不存在");
    }

    @Override
    public MesResult<Integer> updateProcessStepById(ProcessStep processStep) {
        processStep.setModifyTime(new Date());
        return this.updateById(processStep) ? MesResult.success(1) : MesResult.error();
    }

    @Override
    public MesResult<ProcessStep> getProcessStepById(Integer stepId) {
        return MesResult.success(this.getById(stepId));
    }

    @Override
    public MesResult<ProcessStep> saveStep(ProcessStep processStep) {
        return this.save(processStep) ? MesResult.success(processStep) : MesResult.error();
    }

    @Override
    public MesResult<Page<ProcessStep>> listProcessStep(String processCode,int pageNum, int pageSize) {
        Page<ProcessStep> page = new Page<>(pageNum, pageSize);
        QueryWrapper<ProcessStep> wrapper = new QueryWrapper<>();
        wrapper.eq("process_code", processCode);
        return MesResult.success(this.page( page,wrapper));
    }
}
